package zhihui.mobile.ui.express;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.zgm.utils.UiUtils;
import com.zgm.utils.ui.UiTextHeader;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import zhihui.mobile.R;
import zhihui.mobile.bean.express.ExpressHistoryBeanListV2;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.express.ExpressManager;
import zhihui.mobile.manager.user.UserManager;
import zhihui.mobile.presenter.IPresenter;
import zhihui.mobile.ui.BaseFragmentActivity;
import zhihui.mobile.ui.MyOnClickListener;

/**
 * Created by Administrator on 2017/9/16.
 */

public class ExpressHistoryActivity extends BaseFragmentActivity {
    @Bind(R.id.express_history_title_layout)
    UiTextHeader expressHistoryTitleLayout;
    @Bind(R.id.express_history_rv)
    CustomRecyclerView expressHistoryRv;
    @Bind(R.id.express_history_refresh_view)
    PullToRefreshScrollView expressHistoryRefreshView;
    @Bind(R.id.express_history_havemore_view)
    TextView expressHistoryHavemoreView;

    private ExpressHistoryGetAdapter getAdapter;
    private int pageNum = 1;

    @Override
    public void initView() {


    }

    @Override
    protected int getLayoutResId() {
        return R.layout.exprees_history_activity;
    }

    @Override
    protected IPresenter[] getPresenters() {
        return new IPresenter[0];
    }

    @Override
    protected void onInitPresenter() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        //title数据
        expressHistoryTitleLayout.setTitle("包裹历史");
        expressHistoryTitleLayout.setRightText("");
        expressHistoryTitleLayout.setLeftBottonIcon(R.drawable.shop_back);
        expressHistoryTitleLayout.setLeftButtonOnClickListener(new MyOnClickListener(this) {
            @Override
            public void onClick(View v) {
                super.onClick(v);
                finish();
            }
        });
        //内容数据
        expressHistoryRv.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        expressHistoryRv.setHasFixedSize(true);

        ExpressManager.getInstance().initExpressHistoryList(new IHttpCallBackListener() {
            @Override
            public void success(JSONObject result) {
                Gson gson = new Gson();
                ExpressHistoryBeanListV2 historyBeanList = gson.fromJson(result.toString(), ExpressHistoryBeanListV2.class);
                getAdapter = new ExpressHistoryGetAdapter(historyBeanList.getData().getList(), ExpressHistoryActivity.this);
                expressHistoryRv.setAdapter(getAdapter);
                if (historyBeanList.getData().isLastPage()) {
                    expressHistoryHavemoreView.setVisibility(View.VISIBLE);
                    expressHistoryRefreshView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                } else {
                    expressHistoryHavemoreView.setVisibility(View.GONE);
                }
            }

            @Override
            public void failed(String message) {
                UiUtils.ToastInCenter(ExpressHistoryActivity.this, message, Toast.LENGTH_SHORT);
            }
        }, pageNum, UserManager.getInstance().getCurUser().getNo());

        expressHistoryRefreshView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                pageNum = 1;
                ExpressManager.getInstance().initExpressHistoryList(new IHttpCallBackListener() {
                    @Override
                    public void success(JSONObject result) {
                        Gson gson = new Gson();
                        ExpressHistoryBeanListV2 historyBeanList = gson.fromJson(result.toString(), ExpressHistoryBeanListV2.class);
                        getAdapter = new ExpressHistoryGetAdapter(historyBeanList.getData().getList(), ExpressHistoryActivity.this);
                        expressHistoryRv.setAdapter(getAdapter);
                        expressHistoryRefreshView.onRefreshComplete();
                        if (historyBeanList.getData().isLastPage()) {
                            expressHistoryHavemoreView.setVisibility(View.VISIBLE);
                            expressHistoryRefreshView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                        } else {
                            expressHistoryHavemoreView.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void failed(String message) {
                        UiUtils.ToastInCenter(ExpressHistoryActivity.this, message, Toast.LENGTH_SHORT);
                        expressHistoryRefreshView.onRefreshComplete();
                    }
                }, pageNum, UserManager.getInstance().getCurUser().getNo());


            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                ExpressManager.getInstance().initExpressHistoryList(new IHttpCallBackListener() {
                    @Override
                    public void success(JSONObject result) {
                        Gson gson = new Gson();
                        ExpressHistoryBeanListV2 historyBeanList = gson.fromJson(result.toString(), ExpressHistoryBeanListV2.class);

                        getAdapter.getData().addAll((historyBeanList.getData().getList()));
                        getAdapter.notifyDataSetChanged();
                        expressHistoryRefreshView.onRefreshComplete();
                        if (historyBeanList.getData().getList().size() == 0) {
                            pageNum--;
                        }
                        if (historyBeanList.getData().isLastPage()) {
                            expressHistoryHavemoreView.setVisibility(View.VISIBLE);
                            expressHistoryRefreshView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                        } else {
                            expressHistoryHavemoreView.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void failed(String message) {
                        UiUtils.ToastInCenter(ExpressHistoryActivity.this, message, Toast.LENGTH_SHORT);
                        expressHistoryRefreshView.onRefreshComplete();
                    }
                }, pageNum++, UserManager.getInstance().getCurUser().getNo());
            }
        });

    }
}
